package ro.gateway.aida.servlet;

import java.sql.SQLException;
import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

import ro.gateway.aida.admin.PropertyListDB;
import ro.gateway.aida.db.CFServiceBase;
import ro.gateway.aida.db.DBFilesPersMan;
import ro.gateway.aida.db.DBPersistenceToken;
import ro.gateway.aida.db.DBWrapper;
import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.db.StaticIdGenerator;
import ro.gateway.aida.obj.BreadCrumb;
import ro.gateway.aida.obj.db.AIDAActivityObjectDB;
import ro.gateway.aida.obj.db.AIDALocationDB;
import ro.gateway.aida.obj.db.AIDASectorPersMan;
import ro.gateway.aida.obj.db.ActivityDB;
import ro.gateway.aida.obj.db.ActivityStatusDB;
import ro.gateway.aida.obj.db.CountyDB;
import ro.gateway.aida.obj.db.OrganizationDB;
import ro.gateway.aida.obj.db.PersonDB;
import ro.gateway.aida.obj.db.RegionDB;
import ro.gateway.aida.obj.db.SystemPropertyDB;
import ro.gateway.aida.obj.db.TownDB;
import ro.gateway.aida.obj.db.VillageDB;
import ro.gateway.aida.srv.ICFService;
import ro.gateway.aida.srv.IIDGenerator;
import ro.gateway.aida.srv.PropertiesConfigurator;
import ro.gateway.aida.usr.UserDB;
import ro.gateway.aida.utils.SysProp;
import ro.gateway.aida.utils.Utils;
import ro.xblue.translator.TranslatorDBAccessor;


/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: ro-gateway</p>
 *
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro; Mihai Postelnicu, mihai@ro-gateway.org
 * @version 1.0-* @version $Id: LoadApplicationServlet.java,v 1.2 2005/04/04 00:09:02 mihaipostelnicu Exp $
 */
public class LoadApplicationServlet extends HttpServlet {
	public void init() {
		Hashtable services = new Hashtable();
		this.getServletContext().setAttribute(Constants.SERVICES, services);

		System.out.println("#LoadApplicationServlet: creating cfService...");
		ICFService cfService = loadCFService();
		if (cfService != null)
			services.put(Constants.SERVICE_CF, cfService);
		PersistenceToken persistence_token = new DBPersistenceToken(cfService,
			 new StaticIdGenerator(), Constants.DBS1);
		this.getServletContext().setAttribute(PersistenceToken.TOKEN_KEY,
			 persistence_token);
		System.out.println("#LoadApplicationServlet: cfService done");

		//loading system properties :
		System.out.println("#LoadApplicationServlet: loading sys props...");
		SystemPropertyDB spdb = SystemPropertyDB.getManager(persistence_token);

		try {
			Hashtable props=spdb.getAll();
			persistence_token.setProperty("sysProps", props);
			Constants.DEFAULT_LANGUAGE=Utils.getProperty(persistence_token,SysProp.DEFAULT_LANG);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("#LoadApplicationServlet: sysprops done");

		System.out.println("#LoadApplicationServlet: creating ID generators...");
		IIDGenerator idGenerators = loadIdGenerators();

		if (idGenerators != null)
			services.put(Constants.SERVICE_IDGEN, idGenerators);
		System.out.println("#LoadApplicationServlet: id generators done");

		System.out.println("#LoadApplicationServlet: creating breadcrumbs...");
		try {


			this.getServletContext().setAttribute(BreadCrumb.LIST,
				 DBWrapper.getManager(persistence_token).get(new BreadCrumb()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("#LoadApplicationServlet: breadcrumbs done");

		try {
			DBFilesPersMan.getManager(persistence_token).deleteTemporaryFiles();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}

		System.out.println("#LoadApplicationServlet: init done!");
	}

	public ICFService loadCFService() {
		ServletContext application = this.getServletContext();
		//DBConnectorProperties dbc= new DBConnectorProperties(application);
		CFServiceBase result = new CFServiceBase();
		PropertiesConfigurator configurator = new PropertiesConfigurator();

		try {
			result.configure(application);
		} catch (java.io.IOException ioEx) {
			ioEx.printStackTrace();

			return null;
		}
		
		return result;
	}

	public IIDGenerator loadIdGenerators() {
		ServletContext application = this.getServletContext();
		PersistenceToken token = PersistenceManager.tokenLookup(application);
		Hashtable services = (Hashtable) application.getAttribute(Constants.SERVICES);

		if (services == null)
			throw new RuntimeException("#LoadApplicationServlet: services null!!");

		ICFService cfService = (ICFService) services.get(Constants.SERVICE_CF);
		PersistenceToken pers_token = PersistenceManager.tokenLookup(application);

		IIDGenerator idGenerators = ((DBPersistenceToken) pers_token).getIdgenerators();
		idGenerators.registerGenerator(Constants.IDGEN_USERS);
		idGenerators.registerGenerator(Constants.IDGEN_ACTIVITIES);
		idGenerators.registerGenerator(Constants.IDGEN_ORGANIZATIONS);
		idGenerators.registerGenerator(Constants.IDGEN_OBJECTS);
		idGenerators.registerGenerator(Constants.IDGEN_PERSONS);
		idGenerators.registerGenerator(Constants.IDGEN_SECTORS);
		idGenerators.registerGenerator(Constants.IDGEN_REGIONS);
		idGenerators.registerGenerator(Constants.IDGEN_COUNTIES);
		idGenerators.registerGenerator(Constants.IDGEN_CREGIONS);
		idGenerators.registerGenerator(Constants.IDGEN_ACT_STATUS);
		idGenerators.registerGenerator(Constants.IDGEN_TOWNS);
		idGenerators.registerGenerator(Constants.IDGEN_VILLAGES);
		idGenerators.registerGenerator(Constants.IDGEN_PROP_LISTS);
		idGenerators.registerGenerator(Constants.IDGEN_PROP_LIST_ITEMS);
		idGenerators.registerGenerator(Constants.IDGEN_TRANS_ITEMS);

		try {
			long necstId = 1 + UserDB.getManager(pers_token).getLastid();
			System.out.println("\tIIDGENS->NEXT:" + Constants.IDGEN_USERS +
				 ":" + necstId);
			idGenerators.initGenerator(Constants.IDGEN_USERS, necstId);

			idGenerators.initGenerator(Constants.IDGEN_ACTIVITIES,
				 1 + ActivityDB.getManager(pers_token).getLastId());
			idGenerators.initGenerator(Constants.IDGEN_OBJECTS,
				 1 + AIDAActivityObjectDB.getManager(token).getLastId());
			idGenerators.initGenerator(Constants.IDGEN_SECTORS,
				 1 + AIDASectorPersMan.getManager(pers_token).getLastId());
			idGenerators.initGenerator(Constants.IDGEN_ACT_STATUS,
				 1 + ActivityStatusDB.getManager(pers_token).getLastId());
			idGenerators.initGenerator(Constants.IDGEN_ORGANIZATIONS,
				 1 + OrganizationDB.getLastId(cfService, Constants.DBS1));
			idGenerators.initGenerator(Constants.IDGEN_PERSONS,
				 1 + PersonDB.getLastId(cfService, Constants.DBS1));
			idGenerators.initGenerator(Constants.IDGEN_REGIONS,
				 1 + RegionDB.getManager(token).getLastId());
			idGenerators.initGenerator(Constants.IDGEN_COUNTIES,
				 1 + CountyDB.getManager(token).getLastId());
			idGenerators.initGenerator(Constants.IDGEN_CREGIONS,
				 1 + AIDALocationDB.getManager(token).getLastCRegionId());
			idGenerators.initGenerator(Constants.IDGEN_TOWNS,
				 1 + TownDB.getManager(token).getLastId());
			idGenerators.initGenerator(Constants.IDGEN_VILLAGES,
				 1 + VillageDB.getManager(token).getLastId());
			idGenerators.initGenerator(Constants.IDGEN_PROP_LISTS,
				 1 + PropertyListDB.getManager(token).getMaxListId());
			idGenerators.initGenerator(Constants.IDGEN_PROP_LIST_ITEMS,
				 1 + PropertyListDB.getManager(token).getMaxItemId());
			
			idGenerators.initGenerator(Constants.IDGEN_TRANS_ITEMS,
					 1 + TranslatorDBAccessor.getManager(token).getLastId());			
		} catch (SQLException sqlEx) {
			sqlEx.printStackTrace();
		}

		return idGenerators;
	}
}
